/*
 * @Author: cqg
 * @Date: 2020-11-10 23:18:06
 * @LastEditors: cqg
 * @LastEditTime: 2020-11-22 23:11:31
 * @Description: file content
 */
import {
    getFinanceAssetWarehousingPage,
    exportFinanceWarehousing
} from "@/views/wy-asset-management/finance/api/finance-warehousing.js";
import { getDict } from "~/utils";

export default {
    name: "resource-manage",
    provide () {
        return {
            initList: this.query
        };
    },
    data () {
        return {
            // 勾选
            selectDataArr: [],
            loading: false,
            //表单数据
            tableData: [],
            menuList: [{ export: "导出" }],
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 20 // 每页显示多少条
            },
            // 请求参数
            params: {
                current: 1,
                size: 20,
                keyword: ""
            },
            //过滤条件
            filterList: [
                {
                    label: "资产分类",
                    inputType: "dict",
                    dictName: "asset-classification",
                    name: "assetClassificationList"
                },
                {
                    label: "使用状况",
                    inputType: "dict",
                    dictName: "asset-use-status",
                    name: "useStatusList"
                }
            ]
        };
    },
    //载入事件
    mounted () {
        // //查询
        this.query();
    },
    methods: {
        // 菜单点击监听
        handleMenuClick (type) {
            type == "export" && this.exportReg();
        },
        /**
         * 列表导出
         */
        exportReg (params = { size: 9999 }) {
            let req = { ...this.params, ...params };
            exportFinanceWarehousing(req).then(res => {
                this.loading = false;
                // 此处有个坑。这里用content保存文件流，最初是content=res，但下载的test.xls里的内容如下图1，
                // 检查了下才发现，后端对文件流做了一层封装，所以将content指向res.data即可
                // 另外，流的转储属于浅拷贝，所以此处的content转储仅仅是便于理解，并没有实际作用=_=
                const content = res.data;
                const blob = new Blob([content]); // 构造一个blob对象来处理数据
                const fileName = "财务核实不入库记录.xls"; // 导出文件名
                // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
                // IE10以上支持blob但是依然不支持download
                if ("download" in document.createElement("a")) {
                    // 支持a标签download的浏览器
                    const link = document.createElement("a"); // 创建a标签
                    link.download = fileName; // a标签添加属性
                    link.style.display = "none";
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link);
                    link.click(); // 执行下载
                    URL.revokeObjectURL(link.href); // 释放url
                    document.body.removeChild(link); // 释放标签
                } else {
                    // 其他浏览器
                    navigator.msSaveBlob(blob, fileName);
                }
                this.btnSendTem = false;
            });
        },

        /**
         * 查询方法
         * @param param
         */
        query (params = {}) {
            this.loading = true;
            let req = { ...this.params, ...params };
            !req.assetClassificationList && delete req.assetClassificationList;
            !req.useStatusList && delete req.useStatusList;
            getFinanceAssetWarehousingPage(req)
                .then(res => {
                    const { current, size, total, records } = res.data.data;
                    this.tableData = records;
                    this.params = req;
                    this.pager = { total, current, size };
                    this.loading = false;
                })
                .catch(error => {
                    this.$message({
                        type: "error",
                        message: "查询失败"
                    });
                })
                .finally(() => (this.loading = false));
        },

        //  * 码值转换
        dictVal_assetClassification (row, column, cellValue) {
            let obj = getDict("asset-classification").find(
                p => p.value == cellValue
            );
            if (obj) {
                return obj.label;
            }
            return "";
        },
        //  * 码值转换
        dictVal_useStatusList (row, column, cellValue) {
            let obj = getDict("asset-use-status").find(
                p => p.value == cellValue
            );
            if (obj) {
                return obj.label;
            }
            return "";
        }
    }
};
